package com.huluobo.lc.videopagedemo1806a;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cn.jzvd.JzvdStd;

public class MainActivity extends AppCompatActivity {
    private RecyclerView main_recycler_view;
    private List<String> urlList;
    private PagerSnapHelper snapHelper;
    private VideoPageAdapter videoPageAdapter;
    private LinearLayoutManager linearLayoutManager;
    private int currentPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
        addListener();
    }

    private void initView() {
        main_recycler_view = findViewById(R.id.main_recycler_view);
        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        main_recycler_view.setLayoutManager(linearLayoutManager);

        snapHelper = new PagerSnapHelper();
        snapHelper.attachToRecyclerView(main_recycler_view);

        videoPageAdapter = new VideoPageAdapter(this, urlList);
        main_recycler_view.setAdapter(videoPageAdapter);

    }

    private void initData() {
        urlList = new ArrayList<>();
        urlList.add("https://pipijoke.oss-cn-hangzhou.aliyuncs.com/6825960242983999755.mp4");
        urlList.add("https://pipijoke.oss-cn-hangzhou.aliyuncs.com/6827410794234255623.mp4");
        urlList.add("https://pipijoke.oss-cn-hangzhou.aliyuncs.com/6810585660219480333.mp4");
        urlList.add("https://pipijoke.oss-cn-hangzhou.aliyuncs.com/6832487096775612686.mp4");
        urlList.add("https://pipijoke.oss-cn-hangzhou.aliyuncs.com/6812996722365569283.mp4");
        urlList.add("https://pipijoke.oss-cn-hangzhou.aliyuncs.com/6828336780488481035.mp4");
        urlList.add("https://pipijoke.oss-cn-hangzhou.aliyuncs.com/1588996970841.mp4");
        urlList.add("https://pipijoke.oss-cn-hangzhou.aliyuncs.com/1588989579839.mp4");
        urlList.add("https://pipijoke.oss-cn-hangzhou.aliyuncs.com/dabing.mp4");
    }

    private void addListener() {
        main_recycler_view.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_IDLE://停止滚动
                        View view = snapHelper.findSnapView(linearLayoutManager);

                        //当前固定后的item,position
                        assert view != null;
                        int position = recyclerView.getChildAdapterPosition(view);
                        if (currentPosition != position) {
                            //如果当前position 和上一次固定后的position相同,说明是同一个item,只不过是滑动了一点点,没有切换到下一个item
                            JzvdStd.releaseAllVideos();//释放所有资源
                            RecyclerView.ViewHolder viewHolder =
                                    recyclerView.getChildViewHolder(view);
                            if (viewHolder != null && viewHolder instanceof VideoPageAdapter.VideoViewHolder) {
                                ((VideoPageAdapter.VideoViewHolder) viewHolder).jz_video.startVideo();
                            }

                        }
                        currentPosition = position;

                        break;
                    case RecyclerView.SCROLL_STATE_DRAGGING://拖动
                        break;
                    case RecyclerView.SCROLL_STATE_SETTLING://惯性滑动
                        break;
                }

            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        JzvdStd.releaseAllVideos();
    }
}